<?php

/**
 * NumberValidator is subclass of CNumberValidator.
 *
 * The validation will only occur if $dependOn is null or value of $dependOn
 * attribute is evaluate to true.
 *
 * In addition, the min and max value could be overriden by value from database
 * by declaring minAttribute and maxAttribute.
 *
 * Example:
 * value attribute must be in range between minValue and maxValue if hasRange
 * attribute value is true.
 *
 * The rule:
 * array(
 *     'value',
 *     'NumberValidator',
 *     'dependOnAttribute'=>'hasRange',
 *     'minAttribute'=>'minValue',
 *     'maxAttribute'=>'maxValue',
 * )
 *
 * @author Julot
 * @since Wednesday, February 16th, 2011
 */
class NumberValidator extends CNumberValidator {
	/**
	 * @property string $dependOn the attribute name seperated by dot that this
	 *     validator depend on.
	 */
	public $dependOnAttribute = null;
	
	/**
	 * @property string $minAttribute the attribute name seperated by dot that
	 *     will override min value by value from the database.
	 */
	public $minAttribute = null;
	
	/**
	 * @property string $maxAttribute the attribute name seperated by dot that
	 *     will override max value by value from the database.
	 */
	public $maxAttribute = null;
	
	/**
	 * Validate only if $dependOnAttribute is null or value of
	 * $dependOnAttribute is true.
	 * @param CModel $object
	 * @param string $attribute
	 */
	public function validateAttribute($object, $attribute) {
		if ($this->minAttribute)
			$this->min = $object->getAttribute($this->minAttribute);
		if ($this->maxAttribute)
			$this->max = $object->getAttribute($this->maxAttribute);
		
		if ($this->dependOnAttribute) {
			if ($object->getAttribute($this->dependOnAttribute))
				parent::validateAttribute($object, $attribute);
		}
		else parent::validateAttribute($object, $attribute);
	}
}